﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZK.Models;

namespace ZK.FWQPrintTBWindowsService
{
    /// <summary>
    /// 地磅磅单打印类
    /// </summary>
    public class ScalesPrint
    {
        #region 属性
        /// <summary>
        /// 运费
        /// </summary>
        public string YF;
        /// <summary>
        /// 打印的车辆信息
        /// </summary>
        public GetTruckYxInfoRceiveM yxYsTransportReturnDto = new GetTruckYxInfoRceiveM();
        /// <summary>
        /// 公司名称
        /// </summary>
        public string GSMC = "";
        /// <summary>
        /// 纸张宽度单位毫米
        /// </summary>
        private int width_p = 210;
        /// <summary>
        /// 纸张高度 单位毫米
        /// </summary>
        private int height_p = 93;
        /// <summary>
        /// 页面左右边距 单位毫米
        /// </summary>
        private int margin_lr = 2;
        /// <summary>
        /// 页面上下边距 单位毫米
        /// </summary>
        private int margin_tb = 2;
        /// <summary>
        /// 上下居中，左右居中
        /// </summary>
        private StringFormat stringFormatCenterAll = new StringFormat()
        {
            Alignment = StringAlignment.Center,
            LineAlignment = StringAlignment.Center
        };
        /// <summary>
        /// 左对齐
        /// </summary>
        private StringFormat stringFormatCenterLeft = new StringFormat()
        {
            Alignment = StringAlignment.Near,
            LineAlignment = StringAlignment.Center
        };
        /// <summary>
        /// 右对齐
        /// </summary>
        private StringFormat stringFormatCenterRight = new StringFormat()
        {
            Alignment = StringAlignment.Far,
            LineAlignment = StringAlignment.Center
        };
        /// <summary>
        /// 上对齐
        /// </summary>
        private StringFormat stringFormatCenterUp = new StringFormat()
        {
            Alignment = StringAlignment.Center,
            LineAlignment = StringAlignment.Near
        };/// <summary>
          /// 下对齐
          /// </summary>
        private StringFormat stringFormatCenterDown = new StringFormat()
        {
            Alignment = StringAlignment.Center,
            LineAlignment = StringAlignment.Far
        };
        #endregion
        public enum ScalesType
        {
            one = 1,
            two = 2
        }
        /// <summary>
        /// 转换毫米到百分之一英寸
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        private int MM2Inch(double mm)
        {
            return (int)(mm * 100.0f / 25.4f);
        }
        /// <summary>
        /// 打印纸张的初始化
        /// </summary>
        /// <returns></returns>
        public PrintDocument printDocument(ScalesType scalesType)
        {
            try
            {
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                pd.PrinterSettings.PrinterName = Common.AppConfigHelper.GetAppSettings("PrintName");
                pd.DocumentName = pd.PrinterSettings.MaximumCopies.ToString();
                //磅单页面选择
                switch (scalesType)
                {
                    case ScalesType.one:
                        pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.PrintPageOne);
                        break;
                    default:
                        break;
                }

                PrinterSettings printSetting = new PrinterSettings();
                printSetting.PrintRange = PrintRange.AllPages;
                int width_in = MM2Inch(width_p);
                int height_in = MM2Inch(height_p);
                PageSettings pageSetting = new PageSettings(printSetting);
                pageSetting.PaperSize = new PaperSize("customer", width_in, height_in);
                int margin_lr_in = MM2Inch(margin_lr);
                int margin_tb_in = MM2Inch(margin_tb);
                pageSetting.Margins = new Margins(margin_lr_in, margin_lr_in, margin_tb_in, margin_tb_in);
                pd.DefaultPageSettings = pageSetting;
                pd.PrintController = new System.Drawing.Printing.StandardPrintController();
                return pd;
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("ScalesPrint printDocument" + ex.ToString());
                return null;
            }
        }


        public PrintDocument printDocument()
        {
            try
            {
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                pd.PrinterSettings.PrinterName = Common.AppConfigHelper.GetAppSettings("PrintName");
                pd.DocumentName = pd.PrinterSettings.MaximumCopies.ToString();
                //磅单页面选择
                pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.PrintPageOne);
                PrinterSettings printSetting = new PrinterSettings();
                printSetting.PrintRange = PrintRange.AllPages;
                int width_in = MM2Inch(width_p);
                int height_in = MM2Inch(height_p);
                PageSettings pageSetting = new PageSettings(printSetting);
                pageSetting.PaperSize = new PaperSize("customer", width_in, height_in);
                int margin_lr_in = MM2Inch(margin_lr);
                int margin_tb_in = MM2Inch(margin_tb);
                pageSetting.Margins = new Margins(margin_lr_in, margin_lr_in, margin_tb_in, margin_tb_in);
                pd.DefaultPageSettings = pageSetting;
                pd.PrintController = new System.Drawing.Printing.StandardPrintController();
                return pd;
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("ScalesPrint printDocument" + ex.ToString());
                return null;
            }
        }
        /// <summary>
        /// one
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ev"></param>
        private void PrintPageOne(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        {
            if (yxYsTransportReturnDto == null) yxYsTransportReturnDto = new GetTruckYxInfoRceiveM();
            StringFormat stringFormatTitle = new StringFormat();
            stringFormatTitle.Alignment = StringAlignment.Center; //居中
            ev.Graphics.DrawString("运城关铝热电有限公司汽车衡计量单", new Font("宋体", MM2Inch(5)), Brushes.Black, new Rectangle(MM2Inch(0), MM2Inch(12), MM2Inch(210), MM2Inch(90)), stringFormatTitle);//写字
            ev.Graphics.DrawString("序号：" + yxYsTransportReturnDto.wbCode + " 日期：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "         单位：吨",
                new Font("宋体", MM2Inch(3)),
                Brushes.Black,
                new Rectangle(MM2Inch(0), MM2Inch(22), MM2Inch(210), MM2Inch(90)),
                stringFormatTitle
                );//写字

            int QDX = 30;//矩形起点的x坐标
            int QDY = 30;//矩形起点的y坐标
            int HJ = 9;//行距
            int HS = 5;//行数
            int XZ = 1;//线粗（毫米）
            int DJXKD = 150;//大矩形宽度
            int SJZT = 3;//矩形中数据字体的大小
            ev.Graphics.DrawRectangle(new Pen(Brushes.Black, XZ), new Rectangle(MM2Inch(QDX), MM2Inch(QDY), MM2Inch(DJXKD), MM2Inch(HJ * HS)));//矩形
            for (int i = 0; i < 5; i++)
            {
                ev.Graphics.DrawLine(new Pen(Brushes.Black, XZ), MM2Inch(30), MM2Inch(QDY + HJ * (i + 1)), MM2Inch(180), MM2Inch(QDY + HJ * (i + 1)));
                switch (i)
                {
                    //row of 2
                    case 0:
                        ev.Graphics.DrawString("发货单位：",
                            new Font("宋体", MM2Inch(SJZT)),
                            Brushes.Black,
                             new Rectangle(MM2Inch(QDX), MM2Inch(QDY), MM2Inch(22), MM2Inch(HJ)),
                             new StringFormat()
                             {
                                 Alignment = StringAlignment.Center,
                                 LineAlignment = StringAlignment.Center
                             }
                            );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.shipperName,
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + 22), MM2Inch(QDY), MM2Inch(DJXKD / 2 - 22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );

                        ev.Graphics.DrawString("收货单位：",
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + DJXKD / 2), MM2Inch(QDY), MM2Inch(22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.receivingPartyName,
                          new Font("宋体", MM2Inch(SJZT)),
                          Brushes.Black,
                           new Rectangle(MM2Inch(QDX + DJXKD / 2 + 22), MM2Inch(QDY), MM2Inch(DJXKD / 2 - 22), MM2Inch(HJ)),
                           new StringFormat()
                           {
                               Alignment = StringAlignment.Center,
                               LineAlignment = StringAlignment.Center
                           }
                          );

                        break;
                    //row of 2
                    case 1:
                        ev.Graphics.DrawString("车    号：",
                            new Font("宋体", MM2Inch(SJZT)),
                            Brushes.Black,
                             new Rectangle(MM2Inch(QDX), MM2Inch(QDY + HJ), MM2Inch(22), MM2Inch(HJ)),
                             new StringFormat()
                             {
                                 Alignment = StringAlignment.Center,
                                 LineAlignment = StringAlignment.Center
                             }
                            );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.truckNo,
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + 22), MM2Inch(QDY + HJ), MM2Inch(DJXKD / 2 - 22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );

                        ev.Graphics.DrawString("物    资：",
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + DJXKD / 2), MM2Inch(QDY + HJ), MM2Inch(22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.coalName,
                          new Font("宋体", MM2Inch(SJZT)),
                          Brushes.Black,
                           new Rectangle(MM2Inch(QDX + DJXKD / 2 + 22), MM2Inch(QDY + HJ), MM2Inch(DJXKD / 2 - 22), MM2Inch(HJ)),
                           new StringFormat()
                           {
                               Alignment = StringAlignment.Center,
                               LineAlignment = StringAlignment.Center
                           }
                          );

                        break;
                    //row of 3
                    case 2:
                        ev.Graphics.DrawString("毛    重：",
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX), MM2Inch(QDY + HJ * 2), MM2Inch(22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.mz.ToString(),
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + 22), MM2Inch(QDY + HJ * 2), MM2Inch(26.5), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString("司 磅 员：",
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + 22 + 26.5), MM2Inch(QDY + HJ * 2), MM2Inch(22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.mzMan,
                          new Font("宋体", MM2Inch(SJZT)),
                          Brushes.Black,
                           new Rectangle(MM2Inch(QDX + 22 + 26.5 + 22), MM2Inch(QDY + HJ * 2), MM2Inch(26.5), MM2Inch(HJ)),
                           new StringFormat()
                           {
                               Alignment = StringAlignment.Center,
                               LineAlignment = StringAlignment.Center
                           }
                          );
                        ev.Graphics.DrawString("日期时间：",
                         new Font("宋体", MM2Inch(SJZT)),
                         Brushes.Black,
                          new Rectangle(MM2Inch(QDX + 22 + 26.5 + 22 + 26.5), MM2Inch(QDY + HJ * 2), MM2Inch(22), MM2Inch(HJ)),
                          new StringFormat()
                          {
                              Alignment = StringAlignment.Center,
                              LineAlignment = StringAlignment.Center
                          }
                         );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.mzTime,
                         new Font("宋体", MM2Inch(SJZT)),
                         Brushes.Black,
                          new Rectangle(MM2Inch(QDX + 22 + 26.5 + 22 + 26.5 + 22), MM2Inch(QDY + HJ * 2), MM2Inch(31), MM2Inch(HJ)),
                          new StringFormat()
                          {
                              Alignment = StringAlignment.Center,
                              LineAlignment = StringAlignment.Center
                          }
                         );
                        break;
                    //row of 4
                    case 3:
                        ev.Graphics.DrawString("皮    重：",
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX), MM2Inch(QDY + HJ * 3), MM2Inch(22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.pz.ToString(),
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + 22), MM2Inch(QDY + HJ * 3), MM2Inch(26.5), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString("司 磅 员：",
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + 22 + 26.5), MM2Inch(QDY + HJ * 3), MM2Inch(22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.pzMan,
                          new Font("宋体", MM2Inch(SJZT)),
                          Brushes.Black,
                           new Rectangle(MM2Inch(QDX + 22 + 26.5 + 22), MM2Inch(QDY + HJ * 3), MM2Inch(26.5), MM2Inch(HJ)),
                           new StringFormat()
                           {
                               Alignment = StringAlignment.Center,
                               LineAlignment = StringAlignment.Center
                           }
                          );
                        ev.Graphics.DrawString("日期时间：",
                         new Font("宋体", MM2Inch(SJZT)),
                         Brushes.Black,
                          new Rectangle(MM2Inch(QDX + 22 + 26.5 + 22 + 26.5), MM2Inch(QDY + HJ * 3), MM2Inch(22), MM2Inch(HJ)),
                          new StringFormat()
                          {
                              Alignment = StringAlignment.Center,
                              LineAlignment = StringAlignment.Center
                          }
                         );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.pzTime,
                         new Font("宋体", MM2Inch(SJZT)),
                         Brushes.Black,
                          new Rectangle(MM2Inch(QDX + 22 + 26.5 + 22 + 26.5 + 22), MM2Inch(QDY + HJ * 3), MM2Inch(31), MM2Inch(HJ)),
                          new StringFormat()
                          {
                              Alignment = StringAlignment.Center,
                              LineAlignment = StringAlignment.Center
                          }
                         );
                        break;
                    //row of 5
                    case 4:
                        ev.Graphics.DrawString("净    重：",
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX), MM2Inch(QDY + HJ * 4), MM2Inch(22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.jz.ToString(),
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + 22), MM2Inch(QDY + HJ * 4), MM2Inch(26.5), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString("扣    重：",
                           new Font("宋体", MM2Inch(SJZT)),
                           Brushes.Black,
                            new Rectangle(MM2Inch(QDX + 22 + 26.5), MM2Inch(QDY + HJ * 4), MM2Inch(22), MM2Inch(HJ)),
                            new StringFormat()
                            {
                                Alignment = StringAlignment.Center,
                                LineAlignment = StringAlignment.Center
                            }
                           );
                        ev.Graphics.DrawString(yxYsTransportReturnDto.buckleWeight.ToString(),
                          new Font("宋体", MM2Inch(SJZT)),
                          Brushes.Black,
                           new Rectangle(MM2Inch(QDX + 22 + 26.5 + 22), MM2Inch(QDY + HJ * 4), MM2Inch(26.5), MM2Inch(HJ)),
                           new StringFormat()
                           {
                               Alignment = StringAlignment.Center,
                               LineAlignment = StringAlignment.Center
                           }
                          );
                        ev.Graphics.DrawString("实    重：",
                         new Font("宋体", MM2Inch(SJZT)),
                         Brushes.Black,
                          new Rectangle(MM2Inch(QDX + 22 + 26.5 + 22 + 26.5), MM2Inch(QDY + HJ * 4), MM2Inch(22), MM2Inch(HJ)),
                          new StringFormat()
                          {
                              Alignment = StringAlignment.Center,
                              LineAlignment = StringAlignment.Center
                          }
                         );
                        ev.Graphics.DrawString((yxYsTransportReturnDto.jz - yxYsTransportReturnDto.buckleWeight).ToString(),
                         new Font("宋体", MM2Inch(SJZT)),
                         Brushes.Black,
                          new Rectangle(MM2Inch(QDX + 22 + 26.5 + 22 + 26.5 + 22), MM2Inch(QDY + HJ * 4), MM2Inch(31), MM2Inch(HJ)),
                          new StringFormat()
                          {
                              Alignment = StringAlignment.Center,
                              LineAlignment = StringAlignment.Center
                          }
                         );
                        break;

                    default:
                        break;
                }
            }

            for (int i = 0; i < 6; i++)
            {
                switch (i)
                {
                    case 0:
                        ev.Graphics.DrawLine(new Pen(Brushes.Black, 1), MM2Inch(QDX + 22), MM2Inch(QDY), MM2Inch(QDX + 22), MM2Inch(QDY + HJ * HS));
                        break;
                    case 1:
                        ev.Graphics.DrawLine(new Pen(Brushes.Black, 1), MM2Inch(QDX + 22 + 26.5), MM2Inch(QDY + 2 * HJ), MM2Inch(QDX + 22 + 26.5), MM2Inch(QDY + HJ * HS));
                        break;
                    case 2:
                        ev.Graphics.DrawLine(new Pen(Brushes.Black, 1), MM2Inch(QDX + 22 + 26.5 + 22), MM2Inch(QDY + 2 * HJ), MM2Inch(QDX + 22 + 26.5 + 22), MM2Inch(QDY + HJ * HS));
                        break;
                    case 3:
                        ev.Graphics.DrawLine(new Pen(Brushes.Black, 1), MM2Inch(QDX + DJXKD / 2), MM2Inch(QDY), MM2Inch(QDX + DJXKD / 2), MM2Inch(QDY + 2 * HJ));
                        break;
                    case 4:
                        ev.Graphics.DrawLine(new Pen(Brushes.Black, 1), MM2Inch(QDX + 22 + DJXKD / 2), MM2Inch(QDY), MM2Inch(QDX + 22 + DJXKD / 2), MM2Inch(QDY + HJ * HS));
                        break;
                    case 5:
                        ev.Graphics.DrawLine(new Pen(Brushes.Black, 1), MM2Inch(QDX + 22 + DJXKD / 2 + 22), MM2Inch(QDY + 2 * HJ), MM2Inch(QDX + 22 + DJXKD / 2 + 22), MM2Inch(QDY + HJ * HS));
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
